﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Collections.Generic;

using HelixEngine.Maths;

namespace HelixGame.Operations
{
    /// <summary>
    /// 
    /// </summary>
    public class OperationsManager
    {
        Queue<IGameOperation> _operationQueue = new Queue<IGameOperation>();
        IGameOperation _currentOperation = null;
        
        /// <summary>
        /// Updates the operations manager
        /// </summary>
        public void Update(EngineTime time)
        {
            if (_currentOperation == null)
            {
                if (_operationQueue.Count > 0)
                {
                    _currentOperation = _operationQueue.Dequeue();
                    _currentOperation.Initialise();
                }
            }
            else
            {
                if (_currentOperation.Update(time))
                {
                    _currentOperation.Cleanup();
                    _currentOperation = null;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void QueueOperation<T>() where T : IGameOperation, new()
        {
            QueueOperation(new T());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="operation"></param>
        public void QueueOperation(IGameOperation operation)
        {
            _operationQueue.Enqueue(operation);
        }
    }
}
